package easeui.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysm.smfu.R;
import com.ysm.smfu.manager.GlideCircleTransform;
import com.ysm.smfu.utils.UIUtils;

import easeui.controller.EaseUI;
import easeui.domain.EaseUser;

public class EaseUserUtils {

    static EaseUI.EaseUserProfileProvider userProvider;

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * get EaseUser according username
     *
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username) {
        if (userProvider != null)
            return userProvider.getUser(username);

        return null;
    }

    /**
     * 设置用户头像
     */
    public static void setUserAvatar(String imageAddress, ImageView imageView, Context activity) {
        if (!TextUtils.isEmpty(imageAddress) && !imageAddress.startsWith("null?"))
            Glide.with(activity).load(imageAddress)
                    .placeholder(R.drawable.myimg2).error(R.drawable.myimg2)
                    .override(UIUtils.dip2px(45), UIUtils.dip2px(45))
                    .centerCrop()
                    .transform(new GlideCircleTransform(UIUtils.getContext()))
                    .into(imageView);
        else
            imageView.setImageResource(R.drawable.myimg2);
    }

    /**
     * set user's nickname
     */
    public static void setUserNick(String username, TextView textView) {
        if (textView != null) {
//            EaseUser user = getUserInfo(username);
//            if (user != null && user.getNick() != null) {
//                textView.setText(user.getNick());
//            } else {
            textView.setText(username);
//            }
        }
    }
}
